package com.atguigu.boot.controller;

import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpStatusCodeException;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zp
 * @version 1.0
 * @description: 测试异常处理
 * @date 2022/9/19 18:41
 */
@RestController
public class ErrorController {

    @GetMapping("/errorTest")
    public String errTest(){
        int x = 1/0;
        return "error";
    }

    @ExceptionHandler(ArithmeticException.class)
    public Map<String,Object> getArithmeticException(HttpServletRequest request,Exception ex){
        Map<String,Object> map = new HashMap<>();
        map.put("type","ArithmeticException ErrorController");
        map.put("date",new Date());
        map.put("ex",ex.getMessage());
        return map;
    }



    @GetMapping("/errorNull")
    public String errorNull(){
        String x = null;
        x.getBytes();
        return "error";
    }

//    @ExceptionHandler(Exception.class)
//    public Map<String,Object> getNullPointerException(Exception exception){
//        Map<String,Object> map = new HashMap<>();
//        map.put("type","Exception");
//        map.put("date",new Date());
//        map.put("ex",exception.getMessage());
//        return map;
//    }


}
